import { lgRequest } from './utils/ajax'
import util from './utils/util'
// // main.js，注意要在use方法之后执行
// import uView from 'node_modules/uview-ui'
// Vue.use(uView)
// 如此配置即可
// uni.$u.config.unit = 'rpx'
App({
  onLaunch: function (options) {
    let that = this
    wx.getSystemInfo({
      success: function (res) {
        that.globalData.sysInfo = res
        console.log(res)
        that.globalData.statusBarHeight = res.statusBarHeight
        console.log(res.statusBarHeight,'res.statusBarHeight')
        that.globalData.titleBarHeight = wx.getMenuButtonBoundingClientRect().bottom + wx.getMenuButtonBoundingClientRect().top-(res.statusBarHeight * 2)
        let iphoneArr = ['iPhone X', 'iPhone 11', 'iPhone 11 Pro Max', 'iPhone 12', 'iPhone 12 Pro Max', 'iPhone13,2']
        iphoneArr.forEach(element => {
          if (res.model.search(element) != -1) {
            that.globalData.isIphoneX = true
          }
        });
      }
    })
    that.checknNewVersion()
    let tokenInfo = wx.getStorageSync('token')
    if (util.isEmpty(tokenInfo)) {
      wx.reLaunch({
        url: '/pages/login/login'
      })
    }
  },
  //检查是否存在新版本
  checknNewVersion() {
    const updateManager = wx.getUpdateManager()
    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
      // console.log('检查是否存在新版本完成')
    })
    updateManager.onUpdateReady(function () {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好，小程序需要重启以使用最新功能，请重启应用。',
        success: function (res) {
          if (res.confirm) {
            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      })
    })
    updateManager.onUpdateFailed(function () {
      // 新版本下载失败
      // console.log('更新失败')
      wx.showModal({
        title: '发现新版本',
        content: '请删除当前小程序，重新搜索打开...',
      })
    })
  },
  getBaseInfo: function () {
    var that = this;
    let appId = wx.getAccountInfoSync().miniProgram.appId
  },
  // 设置监听器
  watch: function (ctx, obj) {
    Object.keys(obj).forEach(key => {
      this.observer(ctx.data, key, ctx.data[key], function (value) {
        obj[key].call(ctx, value)
      })
    })
  },
  // 监听属性，并执行监听函数
  observer: function (data, key, val, fn) {
    Object.defineProperty(data, key, {
      configurable: true,
      enumerable: true,
      get: function () {
        return val
      },
      set: function (newVal) {
        if (newVal === val) return
        fn && fn(newVal)
        val = newVal
      },
    })
  },


  globalData: {
    // server: 'http://114.115.190.196:8082/',//线上
    // server: 'http://192.168.1.122:8082/',//线下
    // http://114.115.190.196:8082/
    server: 'https://www.mengbaoonline.com:8182/',
    obLineSever:'https://www.mingchuliangzao.com.cn:8632/',
    // hostOrder: 'http://192.168.0.251:1998/',
    // hostAdmin: 'http://192.168.0.251:1999/',
    // host: 'https://lg.fp1123.com/',
    // hostOrder: 'https://lg.fp1123.com/',
    // hostAdmin: 'https://lg.fp1123.com/',
    // hostSearch: 'https://lg.fp1123.com/',
    appId: wx.getAccountInfoSync().miniProgram.appId,
    userInfo: {},
    sysInfo: {},
    isIphoneX: false, // 是否为iphoneX以上系列
    statusBarHeight: 0,
    titleBarHeight: 0,
    companyInfo:'',
    Authorization: wx.getStorageSync('token')
  },
})
